﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.VisualBasic;
using MySql.Data.MySqlClient;
using System.Windows.Threading;


namespace WpfApp2
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string MyConStr = "Server=localhost;Database=aaa;uid=root;pwd=123456";

            MySqlConnection conn = new MySqlConnection(MyConStr);

            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                MessageBox.Show("Connection Opened Successfully");
                conn.Close();
            }
        }
        private int count = 0;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DBConnection conn = new DBConnection();
            // DataSet dataset = conn.Select();
            DataTable dataset = conn.Select();
            if (dataset == null)
            {
                MessageBox.Show("没有数据");
            }
            list1.Items.Clear();
            Console.WriteLine("清空数据："+ count + list1.Items.Count);
            // Thread.Sleep(2000);
            Console.WriteLine("查询到结果的行数:" + dataset.Rows.Count);
            for(var i=0;i< dataset.Rows.Count; i++)
            {
                var row = dataset.Rows[i];
                var str = "";
                for(var j = 0; j < row.ItemArray.Length; j++)
                {
                    str += row[j] + "_";
                }
                Console.WriteLine("row：" + i + " "+str);
                if(count != i)
                {
                    list1.Items.Add(str);
                }
            }
            count++;
            // https://blog.csdn.net/qq_37171353/article/details/78198415
            // grid1.ItemsSource = dataset.DefaultView;

            // 原文链接：https://blog.csdn.net/qq_43026206/article/details/86706431
        }

        private void addBtn_Click(object sender, RoutedEventArgs e)
        {
           
            string str = Interaction.InputBox("as","2","3");
            Console.WriteLine("str" + str);
            
        }
        Window2 w2;
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            /*UserControl1 oneComp = new UserControl1();
            oneComp.InitializeLifetimeService();*/
            w2 = new Window2();

            w2.Owner = this; // 此句在fm.ShowDialog(); 前面
            
            w2.ShowDialog();
            Console.WriteLine("w2.DialogResult: "+w2.DialogResult);
            String user = w2.getUser();
            String pass = w2.getPass();
            Console.WriteLine("结果：" + user + " " + pass);
        }
        private DispatcherTimer timer1;
        //设置定时器          

        public void login(String user, String pass)
        {
            Console.WriteLine("login结果：" + user + " " + pass);
            if(user == "admin" && pass == "123456")
            {
                //启动定时器
                timer1 = new DispatcherTimer();
                timer1.Interval = new TimeSpan(5000000);
                timer1.Tick += new EventHandler(timer_Tick);
                timer1.Start();
                MessageBox.Show("登录成功");
                
                // Thread.Sleep(2500);

                w2.Close();

            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            Console.WriteLine("停止了--");
            // 停止定时器
            timer1.Stop();
            // 向对话框发送按键 Enter
           System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            //System.Windows.Window.
        }
        public void cancelLogin()
        {
            Console.WriteLine("取消登录：");
        }
    }

    class DBConnection
    {
        private MySqlConnection connection;
        private string connectionStr;

        public DBConnection()
        {
            try
            {
                string MyConStr = "Server=localhost;Database=aaa;uid=root;pwd=123456";
                /*connectionStr = ConfigurationManager.ConnectionStrings[MyConStr].ConnectionString;
                connection = new MySqlConnection(connectionStr);*/

                connection = new MySqlConnection(MyConStr);
            }
            catch (Exception exception)
            {
                throw new Exception("DBConnection Constructor:" + exception.Message);
            }
        }

        public DataTable Select()
        {
            try
            {
                connection.Open();
                /*if (connection != null)
                {
                    connection.Open();
                }*/

                Console.WriteLine("---开始查询--");
                string cmdStr = "Select * from students";
                MySqlCommand sqlCmd = new MySqlCommand(cmdStr, connection);
                MySqlDataAdapter sda = new MySqlDataAdapter(sqlCmd);

                DataTable ds = new DataTable();
                sda.Fill(ds);
                return ds;
            }
            catch (Exception exception)
            {
                throw new Exception("SelectMethod:" + exception.Message);
            }
            finally
            {
                connection.Close();
            }
        }
    }
    // 原文链接：https://blog.csdn.net/qq_43026206/article/details/86706431
}